import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { templateId, capabilityCode, pageNo = 1, pageSize = 10 } = query;

  // 能力模板配置数据
  const capabilityTemplateConfigs = [
    // 酒店物理资源模板配置
    {
      id: 1,
      template_id: 1,
      capability_code: 'GUEST_SERVICE',
      enabled: true,
      priority: 80,
      default_config: {
        responseTime: '5min',
        enableAutoEscalation: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      // 关联的能力信息
      capability: {
        id: 5,
        code: 'GUEST_SERVICE',
        name: '客人服务能力',
        description: '提供客人服务请求、客房服务、投诉处理等能力',
        icon: 'ant-design:customer-service-outlined',
        color: '#eb2f96',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 2,
      template_id: 1,
      capability_code: 'HOUSEKEEPING_MANAGEMENT',
      enabled: true,
      priority: 85,
      default_config: {
        cleaningSchedule: 'daily',
        qualityCheckRequired: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 6,
        code: 'HOUSEKEEPING_MANAGEMENT',
        name: '客房清洁能力',
        description: '提供客房清洁计划、清洁状态跟踪、清洁质量检查等能力',
        icon: 'ant-design:clean-outlined',
        color: '#13c2c2',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 3,
      template_id: 1,
      capability_code: 'IOT_DEVICE_MANAGEMENT',
      enabled: true,
      priority: 80,
      default_config: {
        enableRemoteControl: true,
        enableAutomation: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 9,
        code: 'IOT_DEVICE_MANAGEMENT',
        name: 'IoT设备管理能力',
        description: '提供智能门锁、温控器、照明等IoT设备管理能力',
        icon: 'ant-design:setting-outlined',
        color: '#722ed1',
        type: 'technical',
        category: 'iot',
      },
    },
    {
      id: 4,
      template_id: 1,
      capability_code: 'REAL_TIME_COMMUNICATION',
      enabled: true,
      priority: 75,
      default_config: {
        enablePushNotification: true,
        enableSMS: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 10,
        code: 'REAL_TIME_COMMUNICATION',
        name: '实时通信能力',
        description: '提供客房服务、前台沟通等实时消息通信能力',
        icon: 'ant-design:message-outlined',
        color: '#fa8c16',
        type: 'technical',
        category: 'communication',
      },
    },
    {
      id: 5,
      template_id: 1,
      capability_code: 'SECURITY_MONITORING',
      enabled: true,
      priority: 85,
      default_config: {
        enableFacialRecognition: true,
        enableVisitorManagement: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 12,
        code: 'SECURITY_MONITORING',
        name: '安全监控能力',
        description: '提供酒店安全监控、门禁管理、访客登记等能力',
        icon: 'ant-design:safety-outlined',
        color: '#ff4d4f',
        type: 'technical',
        category: 'security',
      },
    },
    // 酒店数字资源模板配置
    {
      id: 6,
      template_id: 2,
      capability_code: 'ROOM_MANAGEMENT',
      enabled: true,
      priority: 90,
      default_config: {
        enableRealTimeSync: true,
        autoStatusTransition: true,
      },
      is_required: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 1,
        code: 'ROOM_MANAGEMENT',
        name: '客房管理能力',
        description: '提供客房状态管理、房态更新、房间分配等核心能力',
        icon: 'ant-design:home-outlined',
        color: '#52c41a',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 7,
      template_id: 2,
      capability_code: 'BOOKING_MANAGEMENT',
      enabled: true,
      priority: 95,
      default_config: {
        enableOverbooking: true,
        maxOverbookingRate: 0.1,
      },
      is_required: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 2,
        code: 'BOOKING_MANAGEMENT',
        name: '预订管理能力',
        description: '提供客房预订、订单处理、预订确认等业务能力',
        icon: 'ant-design:calendar-outlined',
        color: '#1890ff',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 8,
      template_id: 2,
      capability_code: 'CHECKIN_CHECKOUT',
      enabled: true,
      priority: 90,
      default_config: {
        enableAutoCheckout: true,
        lateCheckoutAllowed: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 3,
        code: 'CHECKIN_CHECKOUT',
        name: '入住退房能力',
        description: '提供客人入住登记、退房结算、房卡管理等能力',
        icon: 'ant-design:user-add-outlined',
        color: '#722ed1',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 9,
      template_id: 2,
      capability_code: 'PRICING_ENGINE',
      enabled: true,
      priority: 85,
      default_config: {
        enableDynamicPricing: true,
        updateFrequency: 'hourly',
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 4,
        code: 'PRICING_ENGINE',
        name: '定价引擎能力',
        description: '提供动态定价、价格策略、优惠计算等能力',
        icon: 'ant-design:dollar-outlined',
        color: '#fa8c16',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 10,
      template_id: 2,
      capability_code: 'PMS_INTEGRATION',
      enabled: true,
      priority: 95,
      default_config: {
        syncInterval: '1min',
        enableRealTimeSync: true,
      },
      is_required: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 7,
        code: 'PMS_INTEGRATION',
        name: 'PMS系统集成能力',
        description: '提供与酒店PMS系统的数据同步和业务集成能力',
        icon: 'ant-design:api-outlined',
        color: '#1890ff',
        type: 'technical',
        category: 'integration',
      },
    },
    {
      id: 11,
      template_id: 2,
      capability_code: 'PAYMENT_GATEWAY',
      enabled: true,
      priority: 90,
      default_config: {
        enableRefund: true,
        enablePartialRefund: true,
      },
      is_required: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 8,
        code: 'PAYMENT_GATEWAY',
        name: '支付网关能力',
        description: '提供多种支付方式集成和支付处理能力',
        icon: 'ant-design:credit-card-outlined',
        color: '#52c41a',
        type: 'technical',
        category: 'payment',
      },
    },
    {
      id: 12,
      template_id: 2,
      capability_code: 'DATA_ANALYTICS',
      enabled: true,
      priority: 80,
      default_config: {
        enablePredictiveAnalytics: true,
        enableRealTimeDashboards: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 11,
        code: 'DATA_ANALYTICS',
        name: '数据分析能力',
        description: '提供酒店运营数据分析和商业智能能力',
        icon: 'ant-design:bar-chart-outlined',
        color: '#eb2f96',
        type: 'technical',
        category: 'analytics',
      },
    },
    // 酒店人力资源模板配置
    {
      id: 13,
      template_id: 3,
      capability_code: 'GUEST_SERVICE',
      enabled: true,
      priority: 85,
      default_config: {
        responseTime: '3min',
        enableAutoEscalation: true,
      },
      is_required: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 5,
        code: 'GUEST_SERVICE',
        name: '客人服务能力',
        description: '提供客人服务请求、客房服务、投诉处理等能力',
        icon: 'ant-design:customer-service-outlined',
        color: '#eb2f96',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 14,
      template_id: 3,
      capability_code: 'HOUSEKEEPING_MANAGEMENT',
      enabled: true,
      priority: 80,
      default_config: {
        cleaningSchedule: 'daily',
        qualityCheckRequired: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 6,
        code: 'HOUSEKEEPING_MANAGEMENT',
        name: '客房清洁能力',
        description: '提供客房清洁计划、清洁状态跟踪、清洁质量检查等能力',
        icon: 'ant-design:clean-outlined',
        color: '#13c2c2',
        type: 'business',
        category: 'hotel',
      },
    },
    {
      id: 15,
      template_id: 3,
      capability_code: 'DATA_ANALYTICS',
      enabled: true,
      priority: 75,
      default_config: {
        enablePredictiveAnalytics: false,
        enableRealTimeDashboards: true,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 11,
        code: 'DATA_ANALYTICS',
        name: '数据分析能力',
        description: '提供酒店运营数据分析和商业智能能力',
        icon: 'ant-design:bar-chart-outlined',
        color: '#eb2f96',
        type: 'technical',
        category: 'analytics',
      },
    },
    {
      id: 16,
      template_id: 3,
      capability_code: 'REAL_TIME_COMMUNICATION',
      enabled: true,
      priority: 80,
      default_config: {
        enablePushNotification: true,
        enableSMS: false,
      },
      is_required: false,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
      capability: {
        id: 10,
        code: 'REAL_TIME_COMMUNICATION',
        name: '实时通信能力',
        description: '提供客房服务、前台沟通等实时消息通信能力',
        icon: 'ant-design:message-outlined',
        color: '#fa8c16',
        type: 'technical',
        category: 'communication',
      },
    },
  ];

  // 过滤数据
  let filteredData = capabilityTemplateConfigs.filter(
    (item) => item.deleted === 0,
  );

  if (templateId) {
    filteredData = filteredData.filter(
      (item) => item.template_id === Number(templateId),
    );
  }

  if (capabilityCode) {
    filteredData = filteredData.filter(
      (item) => item.capability_code === capabilityCode,
    );
  }

  // 分页
  const startIndex = (Number(pageNo) - 1) * Number(pageSize);
  const endIndex = startIndex + Number(pageSize);
  const paginatedData = filteredData.slice(startIndex, endIndex);

  return {
    code: 0,
    message: '查询成功',
    data: {
      list: paginatedData,
      total: filteredData.length,
      pageNo: Number(pageNo),
      pageSize: Number(pageSize),
    },
  };
});
